package cn.cdtu.tibetplan.controller;

import java.util.List;

import cn.cdtu.tibetplan.pojo.*;
import cn.cdtu.tibetplan.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.cdtu.tibetplan.service.SchoolService;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * controller
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/school")
public class SchoolController {

    @Autowired
    private SchoolService schoolService;
    private SchoolWithBLOBs schoolWithBLOBs;

    /**
     * 返回全部列表
     *
     * @return
     */
    @RequestMapping("/findAll")
    public List<School> findAll() {
        return schoolService.findAll();
    }

    /**
     * 返回全部列表
     *
     * @return
     */
    @RequestMapping("/list")
    public String list() {
        return "redirect:search?currentPage=1&pageSize=10";
    }

    @RequestMapping("/search")
    public String search(SchoolWithBLOBs school, int currentPage, int pageSize, Model model) {
        List<School> schools = schoolService.findAll();
        model.addAttribute("schools", schools);
        PageResult result = schoolService.findPage(school, currentPage, pageSize);
        PageEnd pageend = new PageEnd(result.getTotal(), result.getRows(), currentPage, pageSize);
        model.addAttribute("pageEnd", pageend);
        return "/School/index";
    }

    /**
     * 添加学校
     *
     * @return
     */
    @RequestMapping("/addSchool")
    public String addSchool() {
        return "/School/add";
    }

    /**
     * 编辑
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(Long id, Model model) {
        SchoolWithBLOBs school = schoolService.show(id.longValue());
        model.addAttribute("edit", school);
        return "/School/edit";
    }

    /**
     * 查看
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/show")
    public String show(Long id, Model model) {
        SchoolWithBLOBs school = schoolService.show(id.longValue());
        model.addAttribute("show", school);
        return "/School/show";
    }

    /**
     * 增加
     *
     * @param school
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Result add(SchoolWithBLOBs school) {
        try {
            school.setUpdatetime(Tools.getTime());
            schoolService.add(school);
            return new Result(true, "增加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "增加失败");
        }
    }

    /**
     * 修改
     *
     * @param school
     * @return
     */
    @ResponseBody
    @RequestMapping("/update")
    public Result update(SchoolWithBLOBs school) {
        try {
            school.setUpdatetime(Tools.getTime());
            schoolService.update(school);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }
    }

    /**
     * 获取实体
     *
     * @param id
     * @return
     */
    @RequestMapping("/findOne")
    public School findOne(Long id) {
        return schoolService.findOne(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Result delete(Long[] ids) {
        try {
            schoolService.delete(ids);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }

}
